package leetcode;

public class LastLongSub {

    public int lengthOfLongestSubstring(String s) {
     if(null==s||"".equals(s))return 0;
     if(s.length()==1)return 1;
     int length=0;
//     for(int i=0;i<s.length()-1;i++){
//    	 for(int j=i+1;j<s.length();j++){
//    		  String str=s.substring(i,j);
//    		  if(str.indexOf(s.substring(j,j+1))!=-1){
//    			  break;
//    		  }else{
//    			  length=length>str.length()+1?length:str.length()+1;
//    		  }
//    	    }
//    	 }
//     return length==0?1:length;
     
     
     String part = "" + s.charAt(0);
     for (int i = 1; i < s.length(); i++) {
         if (part.indexOf(s.charAt(i)) == -1) {
             part = part + s.charAt(i);
         } else {
             int index = part.indexOf(s.charAt(i));
             if (part.length() > length) {
                 length = part.length();
             }
             part = part.substring(index + 1);
             i--;
         }
     }
     return part.length() > length ? part.length() : length;
    }
    
    public static void main(String[] args) {
		LastLongSub l=new LastLongSub();
		//System.out.println(l.lengthOfLongestSubstring("b"));
		System.out.println(l.lengthOfLongestSubstring("abcabcbb"));
		System.out.println(l.lengthOfLongestSubstring("bbbbb"));
		System.out.println(l.lengthOfLongestSubstring("pwwkew"));
	}
}
